﻿
// SourceInsight4Dlg.cpp: 实现文件
//

#include "pch.h"
#include "framework.h"
#include "SourceInsight4.h"
#include "SourceInsight4Dlg.h"
#include "afxdialogex.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#endif


// 用于应用程序“关于”菜单项的 CAboutDlg 对话框

class CAboutDlg : public CDialogEx
{
public:
	CAboutDlg();

// 对话框数据
#ifdef AFX_DESIGN_TIME
	enum { IDD = IDD_ABOUTBOX };
#endif

	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持

// 实现
protected:
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
{
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()


// CSourceInsight4Dlg 对话框



CSourceInsight4Dlg::CSourceInsight4Dlg(CWnd* pParent /*=nullptr*/)
	: CDialogEx(IDD_SOURCEINSIGHT4_DIALOG, pParent)
	, m_sSerial(_T(""))
	, m_sName(_T(""))
	, m_sOrg(_T(""))
	, m_sEmail(_T(""))
{
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CSourceInsight4Dlg::DoDataExchange(CDataExchange* pDX)
{
	CDialogEx::DoDataExchange(pDX);
	DDX_Text(pDX, EDIT_SERIAL, m_sSerial);
	DDX_Text(pDX, EDIT_NAME, m_sName);
	DDX_Text(pDX, EDIT_Org, m_sOrg);
	DDX_Text(pDX, EDIT_Email, m_sEmail);
}

BEGIN_MESSAGE_MAP(CSourceInsight4Dlg, CDialogEx)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(BTN_REGINFO, &CSourceInsight4Dlg::OnBnClickedReginfo)
END_MESSAGE_MAP()


// CSourceInsight4Dlg 消息处理程序

BOOL CSourceInsight4Dlg::OnInitDialog()
{
	CDialogEx::OnInitDialog();

	// 将“关于...”菜单项添加到系统菜单中。

	// IDM_ABOUTBOX 必须在系统命令范围内。
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != nullptr)
	{
		BOOL bNameValid;
		CString strAboutMenu;
		bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
		ASSERT(bNameValid);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// 设置此对话框的图标。  当应用程序主窗口不是对话框时，框架将自动
	//  执行此操作
	SetIcon(m_hIcon, TRUE);			// 设置大图标
	SetIcon(m_hIcon, FALSE);		// 设置小图标

	// TODO: 在此添加额外的初始化代码

	srand((unsigned int)time(NULL));

	InitCharacterTable();

	return TRUE;  // 除非将焦点设置到控件，否则返回 TRUE
}

void CSourceInsight4Dlg::InitCharacterTable()
{
	int i = 0;
    for (char c = '0'; c <= '9'; ++c)
    {
        m_aCharacterTable[i++] = c;
    }
    for (char c = 'A'; c <= 'Z'; ++c)
    {
        m_aCharacterTable[i++] = c;
    }
}

void CSourceInsight4Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialogEx::OnSysCommand(nID, lParam);
	}
}

// 如果向对话框添加最小化按钮，则需要下面的代码
//  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序，
//  这将由框架自动完成。

void CSourceInsight4Dlg::OnPaint()
{
	if (IsIconic())
	{
		CPaintDC dc(this); // 用于绘制的设备上下文

		SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);

		// 使图标在工作区矩形中居中
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// 绘制图标
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialogEx::OnPaint();
	}
}

//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CSourceInsight4Dlg::OnQueryDragIcon()
{
	return static_cast<HCURSOR>(m_hIcon);
}


void CSourceInsight4Dlg::OnBnClickedReginfo()
{
    char szActId[16] = { 0 };
	char szPath[MAX_PATH] = { 0 };
	
    CSourceInsightTool::GenerateActId(szActId);

    // TODO: 在此添加控件通知处理程序代码
    GenerateRandomSerial();
	GenerateRandomUserName();
	GenerateRandomOrganization();
	GenerateRandomEmail();
	GetDate();

	
	GetModuleFileName(NULL, szPath, sizeof(szPath));
	int i = strlen(szPath) - 1;
	while (szPath[i] != '\\')
	{
		--i;
	}
    szPath[i + 1] = 0;
    strcat_s(szPath, sizeof(szPath), "si4.lic");

    CSourceInsightTool::GenerateSignatureFile(szPath,
        szActId,
        m_sSerial.GetBuffer(),
        m_sName.GetBuffer(),
        m_sOrg.GetBuffer(),
        m_sEmail.GetBuffer(),
        m_sDate.GetBuffer()
    );

	CSourceInsightTool::TurnOffNetworkVerification();

    UpdateData(FALSE);
}

void CSourceInsight4Dlg::GenerateRandomSerial()
{
	int nCharacterIndex = 0;
	char szSerial[20] = { 0 };
	char aRandomChar[7] = { 0 };

	for (int i = 0; i < sizeof(aRandomChar); ++i)
	{
		nCharacterIndex = rand() % sizeof(m_aCharacterTable);
		aRandomChar[i] = m_aCharacterTable[nCharacterIndex];
	}

	sprintf_s(szSerial, sizeof(szSerial), "S4SG-%cR%c%c-%c%c%c%c-",
		aRandomChar[0], 
		aRandomChar[1], 
		aRandomChar[2], 
		aRandomChar[3], 
		aRandomChar[4], 
		aRandomChar[5], 
		aRandomChar[6]
	);

	//strcpy(szSerial, "S4SG-ARCD-EFGH-36V6");
	CSourceInsightTool::GenerateSerial(szSerial);
	m_sSerial = szSerial;
}

void CSourceInsight4Dlg::GenerateRandomUserName()
{
#define USERNAME_LENGTH 8
	char szUserName[USERNAME_LENGTH] = { 0 };
	int nCharacterIndex = 0;

	for (int i = 0; i < USERNAME_LENGTH - 1; ++i)
	{
		nCharacterIndex = rand() % sizeof(m_aCharacterTable);
		szUserName[i] = m_aCharacterTable[nCharacterIndex];
	}
	m_sName = szUserName;
}

void CSourceInsight4Dlg::GenerateRandomOrganization()
{
#define ORGANIZATION_LENGTH 8
    char szOrganizationName[ORGANIZATION_LENGTH] = { 0 };
    int nCharacterIndex = 0;

    for (int i = 0; i < USERNAME_LENGTH - 1; ++i)
    {
        nCharacterIndex = rand() % sizeof(m_aCharacterTable);
        szOrganizationName[i] = m_aCharacterTable[nCharacterIndex];
    }
    m_sOrg = szOrganizationName;
}

void CSourceInsight4Dlg::GenerateRandomEmail()
{
#define EMAIL_LENGTH 20
#define EMAIL_PREFIX_LENGTH 8
    char szEmailName[EMAIL_LENGTH] = { 0 };
	char aRandomCharacters[EMAIL_PREFIX_LENGTH] = { 0 };
    int nCharacterIndex = 0;

    for (int i = 0; i < EMAIL_PREFIX_LENGTH; ++i)
    {
        nCharacterIndex = rand() % sizeof(m_aCharacterTable);
		aRandomCharacters[i] = m_aCharacterTable[nCharacterIndex];
    }
	sprintf_s(szEmailName, sizeof(szEmailName), "%c%c%c%c%c%c%c%c@gmail.com",
		aRandomCharacters[0],
		aRandomCharacters[1],
		aRandomCharacters[2],
		aRandomCharacters[3],
		aRandomCharacters[4],
		aRandomCharacters[5],
		aRandomCharacters[6],
		aRandomCharacters[7]
	);

    m_sEmail = szEmailName;
}

void CSourceInsight4Dlg::GetDate()
{
    struct tm t;   //tm结构指针
    time_t now;  //声明time_t类型变量
	char szDate[12] = { 0 };

    time(&now);      //获取系统日期和时间
    localtime_s(&t, &now);   //获取当地日期和时间
	sprintf_s(szDate, sizeof(szDate), "%04d-%02d-%02d", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday);
	m_sDate = szDate;
}


